package Server.map;

import java.util.Random;

public class Box {
	private final Integer X;
	private final Integer Y;
	public Integer getX() {
		return this.X;
	}
	public Integer getY() {
		return this.Y;
	}

	public Box(int x, int y, boolean isWater){
		this.X = x;
		this.Y = y;
		this.water = isWater;
	}
	public Box(Box b){
		this.X = b.X;
		this.Y = b.Y;
		this.water = b.isWater();
		if (b.isMeatBox())
			this.food = new Meat((Meat)b.food);
		if (b.isVegetableBox())
			this.food = new Vegetable((Vegetable)b.food);
	}
	
	private Boolean water;
	public Boolean isWater(){
		return this.water;
	}
	
	
	private Food food;
	public boolean isMeatBox() {
		if (this.food != null && this.food.isMeat())
			return true;
		return false;
	}
	public boolean isVegetableBox() {
		if (this.food != null && ! this.food.isMeat())
			return true;
		return false;
	}
	public Integer getFoodEnergy() {
		if (this.food != null)
			return this.food.energy;
		return 0;
	}
	public Integer getMaxFoodEnergy(){
		if (this.food != null)
			return this.food.maxEnergy;
		return 0;
	}
	
	public boolean isLand(){
		if (this.food == null && ! this.isWater())
			return true;
		return false;
	}
	
	/*
	 * I metodi devono scegliere a caso l'energia iniziale (massima)
	 * e poi instanziare
	 */
	public void putMeat(){
		Random r = new Random();
		int energy = r.nextInt(300) + 350;
		this.food = new Meat(energy, energy);
	}
	public void putMeat(int energy, int maxEnergy){
		this.food = new Meat(energy, maxEnergy);
	}
	
	public void putVegetable(){
		Random r = new Random();
		int energy = r.nextInt(200) + 150;
		this.food = new Vegetable(energy, energy);
	}
	public void putVegetable(int energy, int maxEnergy){
		this.food = new Vegetable(energy, maxEnergy);
	}
	public void updateFoodEnergy() throws MeatExhaustedException{
		if (this.food != null)
			this.food.update();
	}
	public void foodEaten(){
		if (this.food != null)
			if (this.food.isMeat())
				this.food = null;
			else
				this.food.eaten();
	}


	@Override
	public boolean equals(Object b){
		if (b instanceof Box){
			Box box = (Box) b;
			if (this.X == box.X && this.Y == box.Y)
				return true;
		}
		return false;
	}
	@Override
	public int hashCode(){
		return 31 * this.X + this.Y.hashCode();
	}

}
